package com.project.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("session")
public class SessionController {

    @RequestMapping("test")
    @ResponseBody
    public String  test(HttpServletRequest req){
        //如果服务器中有该用户对应的session对象那么就取出来。
        //否则，为该用户创建对应的session对象，然后再取出。
        HttpSession session = req.getSession();

        return session.getId();
    }

    @RequestMapping("speak")
    public String  speak(HttpServletRequest req){
        req.setAttribute("req","requestInfo");
        //得到session会话对象
        HttpSession session = req.getSession();
        session.setAttribute("ses","sessionInfo");

        return "redirect:/session/index";
    }

    @RequestMapping("/getStr")
    @ResponseBody
    public String getStr(HttpServletRequest req){

        return req.getAttribute("req")+"  "+ req.getSession().getAttribute("ses");
    }

    @RequestMapping("/index")
    public String getIndex(){
        return "/index.html";
    }
}
